package com.stfalcon.chatkit.sample.features.demo;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.stfalcon.chatkit.sample.R;
import com.stfalcon.chatkit.sample.frame.UserFrame;
import com.stfalcon.chatkit.sample.layout.BottomBarItem;
import com.stfalcon.chatkit.sample.layout.BottomBarLayout;

import java.util.ArrayList;
import java.util.List;

public class FrameActivity extends AppCompatActivity {

    private List<Fragment> mFragmentList = new ArrayList<Fragment>();
    private FrameLayout mFlContent;
    private BottomBarLayout mBottomBarLayout;

    private int[] mNormalIconIds = new int[]{
            R.mipmap.tab_home_normal, R.mipmap.tab_video_normal,
            R.mipmap.tab_micro_normal, R.mipmap.tab_me_normal
    };

    private int[] mSelectedIconIds = new int[]{
            R.mipmap.tab_home_selected, R.mipmap.tab_video_selected,
            R.mipmap.tab_micro_selected, R.mipmap.tab_me_selected
    };

    private int[] mTitleIds = new int[]{
            R.string.tab_message,
            R.string.tab_contacts
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom);

        initView();
        initData();
        initListener();
    }

    private void initView() {
        getSupportActionBar().setTitle(R.string.user_list);
        mFlContent = findViewById(R.id.fl_content);
        mBottomBarLayout = findViewById(R.id.bbl);
    }

    private void initData() {
        for (int i = 0; i < mTitleIds.length; i++) {
            //创建item
            BottomBarItem item = createBottomBarItem(i);
            mBottomBarLayout.addItem(item);

            UserFrame homeFragment = new UserFrame(this);
            mFragmentList.add(homeFragment);
        }

        changeFragment(0); //默认显示第一页
    }

    @NonNull
    private TabFragment createFragment(int titleId) {
        TabFragment homeFragment = new TabFragment();
        Bundle bundle = new Bundle();
        bundle.putString(TabFragment.CONTENT, getString(titleId));
        homeFragment.setArguments(bundle);
        return homeFragment;
    }

    private BottomBarItem createBottomBarItem(int i) {
        BottomBarItem item = new BottomBarItem.Builder(this)
                .titleTextBold(true)
                .titleTextSize(8)
                .titleNormalColor(R.color.tab_normal_color)
                .titleSelectedColor(R.color.green)
                //              .openTouchBg(false)
                //              .marginTop(5)
                //              .itemPadding(5)
                //              .unreadNumThreshold(99)
                //              .unreadTextColor(R.color.white)

                //还有很多属性，详情请查看Builder里面的方法
                //There are still many properties, please see the methods in the Builder for details.
                .create(mNormalIconIds[i], mSelectedIconIds[i], getString(mTitleIds[i]));
        return item;
    }

    private void initListener() {
        mBottomBarLayout.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
            @Override
            public void onItemSelected(final BottomBarItem bottomBarItem, int previousPosition, final int currentPosition) {
                Log.i("MainActivity", "position: " + currentPosition);

                changeFragment(currentPosition);
            }
        });

        mBottomBarLayout.getBottomItem(0).setSelected(true);
    }

    private void changeFragment(int currentPosition) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_content, mFragmentList.get(currentPosition));
        transaction.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_dynamic, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.action_add_item:

                Intent intent = new Intent(this, UserSearchActivity.class);

                startActivity(intent);

//                int random = new Random().nextInt(3);
//                //addFragment
//                mFragmentList.add(createFragment(mTitleIds[random]));
//                //addItem
//                BottomBarItem bottomBarItem = createBottomBarItem(random);
//                mBottomBarLayout.addItem(bottomBarItem);
//
//                mBottomBarLayout.setCurrentItem(mFragmentList.size() - 1);
                break;
//            case R.id.action_remove_item:
//                //移除条目
//                mBottomBarLayout.removeItem(0);
//
//                if (mFragmentList.size() != 0) {
//                    mFragmentList.remove(0);
//
//                    if (mFragmentList.size() != 0) {
//                        mBottomBarLayout.setCurrentItem(0);
//                    }
//                }
//                break;
        }
        return super.onOptionsItemSelected(item);
    }



}

